// pages/order/orderwrite/orderwrite.js
const App = getApp()
let AJAX = require('../../../utils/util.js').AJAX
let timeTranfer = require('../../../utils/util.js').timeTranfer
Page({

   /**
    * 页面的初始数据
    */
   data: {
      navH: '', 
      proid: '',
      timetable_id:'',
      productData: '',
      num: 1,
      agree: false,
      studentList: [],
      concatname: '',
      concatphone: '',
      chooseSize: false,
      animationData: {},
      stock_all: 0, 
      payPopUp: {
         submited: {
            pay_number: 1,
         },
         addBtn: false,
         minusBtn: true,
      },
      pic: '',
      total_pic: '',     
      coupon_num: true,
      coupon_name: "", 
      coupon_id:'',
      coupon_jian:false,
      total_pic:0,
      total_pic_jian:0,
      total_jian:0
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
     
      this.setData({
         navH: App.globalData.navHeight,
         proid: options.id || '',
         timetable_id: options.timeable_id
      })
      this.requestInfo()     
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
      if (wx.getStorageSync('student')) {
         let _data = this.data.studentList
         let _i = JSON.parse(wx.getStorageSync('student')).index
         if (_i == 'add') {
            _data.push(JSON.parse(wx.getStorageSync('student')).list)
         } else {
            _data.splice(_i, 1, JSON.parse(wx.getStorageSync('student')).list)
         }

         this.setData({
            studentList: _data
         })
         wx.removeStorageSync('student')
      }
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },
   chooseSezi: function(e) {
      // 用that取代this，防止不必要的情况发生
      var that = this;
      // 创建一个动画实例
      var animation = wx.createAnimation({
         // 动画持续时间
         duration: 500,
         // 定义动画效果，当前是匀速
         timingFunction: 'linear'
      })
      // 将该变量赋值给当前动画
      that.animation = animation
      // 先在y轴偏移，然后用step()完成一个动画
      animation.translateY(200).step()
      // 用setData改变当前动画
      that.setData({
         // 通过export()方法导出数据
         animationData: animation.export(),
         // 改变view里面的Wx：if
         chooseSize: true
      })
      // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
      setTimeout(function() {
         animation.translateY(0).step()
         that.setData({
            animationData: animation.export()
         })
      }, 200)
   },
   // 关闭弹窗
   hideModal: function(e) {
      var that = this;
      var animation = wx.createAnimation({
         duration: 1000,
         timingFunction: 'linear'
      })
      that.animation = animation
      animation.translateY(200).step()
      that.setData({
         animationData: animation.export()

      })
      setTimeout(function() {
         animation.translateY(0).step()
         that.setData({
            animationData: animation.export(),
            chooseSize: false
         })
      }, 200)
   },
   // 获取数据end
   requestInfo() {
      let vm = this
      AJAX("product/order-info", "GET", {
         id: vm.data.proid,
         timetable_id: vm.data.timetable_id
      }).then(res => {
         console.log(res.data)
         let stock_all = parseInt(res.data.store_count) - parseInt(res.data.sales_count)
         //store_count - sales_count
         res.data.begin_time = timeTranfer(res.data.begin_time, 1)
         res.data.sales_price = parseInt(res.data.sales_price)
         res.data.coupons.forEach((item, index) => {
            //console.log(item)
            
            item.floor_price = parseInt(item.floor_price)
            item.price = parseInt(item.price)
            item.start_time = timeTranfer(item.start_time, 1)
            item.end_time = timeTranfer(item.end_time, 1)
         })
         vm.setData({
            productData: res.data, //stock
            stock_all: stock_all,
            pic: res.data.sales_price,
            total_pic: res.data.sales_price,
            total_jian: res.data.sales_price
         })
      })
   },
   // 获取数据end

   // 增加
   addCount: function(e) {
      var onOff = this.data.onOff
      this.setData({
         onOff: false
      });
      let pay_number = this.data.payPopUp.submited.pay_number;
      //console.log(e.currentTarget.dataset.num)
      if (pay_number != this.data.stock_all) {
         // console.log(this.data.stock_all - e.currentTarget.dataset.num)
         this.setData({
            ['payPopUp.submited.pay_number']: ++pay_number,
            ['payPopUp.minusBtn']: false,
            total_pic: e.currentTarget.dataset.num * parseInt(this.data.productData.sales_price)
          
         })
         if (pay_number == this.data.stock_all) {
            this.setData({
               ['payPopUp.addBtn']: true
            })
         }
      }
   },

   // 减少
   minusCount: function(e) {
      let pay_number = this.data.payPopUp.submited.pay_number;
      if (pay_number != 1) {
         // console.log()
         // console.log(this.data.total_pic = this.data.total_pic - this.data.pic )
         // console.log(this.data.stock_all + e.currentTarget.dataset.num)
         this.setData({
            ['payPopUp.submited.pay_number']: --pay_number,
            ['payPopUp.addBtn']: false,
            total_pic: this.data.total_pic - this.data.pic
         })
         if (pay_number == 1) {
            this.setData({
               ['payPopUp.minusBtn']: true
            })
         }
      }
   },
   // 点击使用关闭弹窗
   jump(e) {     
  
      var that = this;
     
          let totalPic= that.data.total_pic - e.currentTarget.dataset.pic
      console.log(totalPic)
      var animation = wx.createAnimation({
         duration: 1000,
         timingFunction: 'linear'
      })
      that.animation = animation
      animation.translateY(200).step()
      that.setData({
         animationData: animation.export(),
         coupon_id: e.currentTarget.dataset.coupon,
         coupon_jian:true,
         total_pic_jian: e.currentTarget.dataset.pic,
         total_jian: totalPic

      })
      setTimeout(function() {
         animation.translateY(0).step()
         that.setData({
            animationData: animation.export(),
            chooseSize: false,
            coupon_num: false,
            coupon_name: e.currentTarget.dataset.name
         })
      }, 200)

   },
   agreeClick() {
      let _val = this.data.agree
      this.setData({
         agree: !_val
      })
   },
   toAddstudent() {
      wx.navigateTo({
         url: '/pages/order/studentinfo/studentinfo',
      })
   },
   submitOrder() {
      let vm = this
      if (!vm.data.agree) {
         wx.showToast({
            title: '请阅读并且同意相关条列',
            icon: "none"
         })
         return
      }
      if (!vm.data.concatname) {
         wx.showToast({
            title: '请填写联系人姓名',
            icon: "none"
         })
         return
      }
      if (!vm.data.concatphone) {
         wx.showToast({
            title: '请填写联系人电话',
            icon: "none"
         })
         return
      }
      var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!myreg.test(vm.data.concatphone)) {
         wx.showToast({
            title: '联系人电话格式不正确',
            icon: "none"
         })
         return;
      }
      AJAX("order/create-order", 'POST', {
         product_id: vm.data.proid,
         num: vm.data.payPopUp.submited.pay_number,
         timetable_id: vm.data.timetable_id,
         customer: JSON.stringify(vm.data.studentList),
         coupon_id: vm.data.coupon_id,
         concatname: vm.data.concatname,
         concatphone: vm.data.concatphone
      }).then(res => {
         console.log(res.data)
         console.log(this.data.proid)
         wx.navigateTo({
            url: '/pages/order/orderinfo/orderinfo?sn=' + res.data.order_sn,
         })
      })
   },
   navBack() {
      wx.navigateBack()
   },
   inputInfo(e) {
      let _type = e.currentTarget.dataset.type
      this.setData({
         [_type]: e.detail.value
      })
   },
   editTap(e) {
      let _index = e.currentTarget.dataset.index
      let _data = this.data.studentList[_index]
      let _obj = {
         index: _index,
         list: _data
      }
      wx.setStorageSync("studentedit", JSON.stringify(_obj))
      wx.navigateTo({
         url: '/pages/order/studentinfo/studentinfo',
      })
   },
   deleteTap(e) {
      let _index = e.currentTarget.dataset.index
      let _data = this.data.studentList
      _data.splice(_index, 1)
      this.setData({
         studentList: _data
      })
   },
   toRegulation() {
      wx.navigateTo({
         url: '/pages/order/regulation/regulation',
      })
   }
})